package com.example.medicine.controller;

import com.example.medicine.Bean.SystemRoleFunction;
import com.example.medicine.Mapper.SystemFunctionMapper;
import com.example.medicine.Mapper.SystemRoleFunctionMapper;
import com.example.medicine.Mapper.SystemRoleMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@Slf4j
@Controller
public class SystemRoleFunctionController {
    @Autowired
    private SystemFunctionMapper systemFunctionMapper;
    @Autowired
    private SystemRoleFunctionMapper systemRoleFunctionMapper;
    @Autowired
    private SystemRoleMapper systemRoleMapper;

    @RequiresPermissions("角色功能")
    @RequestMapping("/roleFun")
    public String showroleFun(Model model){
        log.info("showroleFun角色功能列表页面");
        model.addAttribute("roleName",systemRoleMapper.getAll());
        model.addAttribute("funName",systemFunctionMapper.getAll());
        return "pages/SystemRoleFunction/list";
    }

    @RequiresPermissions("角色功能")
    @GetMapping("/roleFun/{roleName}")
    @ResponseBody
    public Set<String> getFunByRole(@PathVariable String roleName){
        log.info("getFunByRole获取角色的功能信息");
        String roleId = systemRoleMapper.getIdByName(roleName);
        Set<String> functionIds = systemRoleFunctionMapper.getFunIdByRoleId(roleId);

        // 将功能（权限）ID转换成权限名字
        return functionIds.stream()
                .map(functionId -> systemFunctionMapper.getNameById(functionId))
                .collect(Collectors.toSet());
    }

    @RequiresPermissions("角色功能")
    @PostMapping("/roleFun")
    @ResponseBody
    public ResponseEntity<String> receiveSelectedValues(@RequestBody Map<String, Object> requestData) {
        log.info("receiveSelectedValues修改角色的功能信息");
        List<String> funName = (List<String>) requestData.get("selectedValues");
        List<String> funID = funName.stream()
                .map(functionName -> systemFunctionMapper.getIdByName(functionName))
                .toList();
        String roleName = (String) requestData.get("name");
        String roleId = systemRoleMapper.getIdByName(roleName);
        try {
            systemRoleFunctionMapper.deleteByRoleID(roleId);
            for (String x : funID) {
                SystemRoleFunction systemRoleFunction = new SystemRoleFunction();
                systemRoleFunction.setRoleID(roleId);
                systemRoleFunction.setFunctionID(x);
                systemRoleFunctionMapper.insert(systemRoleFunction);
            }
        }
        catch (Exception e) {
            e.printStackTrace(); // 添加错误日志
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"message\": \"发生错误！请联系管理员！\"}");
        }
        return ResponseEntity.ok().body("{\"message\": \"提交成功!\"}");
    }

}
